
package com.wyzz.hr.bo.impl;

import java.io.File;
import java.util.*;
import org.apache.axis.utils.StringUtils;
import org.hibernate.FetchMode;
import org.hibernate.Hibernate;
import org.hibernate.criterion.*;

import com.wyzz.hr.base.BaseAction;
import com.wyzz.hr.base.FileOperate;
import com.wyzz.hr.base.Status;
import com.wyzz.hr.bo.IRecruitapplierBo;
import com.wyzz.hr.dao.IRecruitapplierDAO;
import com.wyzz.hr.dao.impl.StatusDAOImpl;
import com.wyzz.hr.pojo.Employee;
import com.wyzz.hr.pojo.Recruitapplier;
import com.wyzz.hr.pojo.Recruitplan;
import com.wyzz.hr.spring.SpringBeanFactory;
import com.wyzz.hr.util.BaseCrit;
import com.wyzz.hr.util.Pager;
import com.wyzz.hr.util.impl.PropertiesFileConfigManager;

public class RecruitapplierBoImpl implements IRecruitapplierBo, Status {

	public RecruitapplierBoImpl() {
	}

	public List getObjects(Class c, String fetch[]) {
		return applierDAO.getObjects(c, fetch);
	}

	public Recruitapplier loadApplier(Object id, String fetch[]) {
		return (Recruitapplier) applierDAO.loadObject(
				com.wyzz.hr.pojo.Recruitapplier.class, id, fetch,
				new boolean[0]);
	}

	public List updateApplier(Recruitapplier applier, String empNo) {
		List errors = new ArrayList();
		try {
			applier.setRecaLastChangeBy(new Employee(empNo));
			applier.setRecaLastChangeTime(new Date());
			applierDAO.updateObject(applier);
			return errors;
		} catch (Exception e) {
			e.printStackTrace();
		}
		errors.add("\u66F4\u65B0\u7533\u8BF7\u8005\u5931\u8D25!");
		return errors;
	}

	public List deleteApplier(String id[]) {
		List errors = new ArrayList();
		DetachedCriteria dc = DetachedCriteria
				.forClass(com.wyzz.hr.pojo.Recruitapplier.class);
		dc.add(Restrictions.in(Recruitapplier.PROP_ID, id));
		List result = applierDAO.findByCriteria(dc);
		List deleteList = new ArrayList();
		try {
			Recruitapplier applier;
			for (Iterator i$ = result.iterator(); i$.hasNext(); deleteList
					.add(applier)) {
				applier = (Recruitapplier) i$.next();
				String filepath = (new StringBuilder()).append(
						FileOperate.getFileHomePath()).append(
						PropertiesFileConfigManager.getInstance().getProperty(
								"sys.recruitment.applier.path")).toString();
				filepath = !filepath.endsWith("\\") && !filepath.endsWith("/") ? (new StringBuilder())
						.append(filepath).append("/").toString()
						: filepath;
				String fileName = applier.getRecaAttachmentName();
				String file = (new StringBuilder()).append(filepath).append(
						fileName).toString();
				File f = new File(file);
				if (f.exists())
					f.delete();
			}

			applierDAO.deleteCollection(deleteList);
		} catch (Exception e) {
			errors.add("\u5220\u9664\u5931\u8D25\uFF01");
		}
		return errors;
	}

	public List searchApplierWithoutId(Recruitapplier applier, Pager page) {
		DetachedCriteria dc = DetachedCriteria
				.forClass(com.wyzz.hr.pojo.Recruitapplier.class);
		dc.createAlias(Recruitapplier.PROP_RECA_CREATE_BY, "recaCreateBy", 1);
		dc.createAlias(Recruitapplier.PROP_RECA_LAST_CHANGE_BY,
				"recaLastChangeBy", 1);
		dc.createAlias(Recruitapplier.PROP_RECA_PLAN_ID, "recaPlanId", 1);
		dc.createAlias(Recruitapplier.PROP_RECA_CHANNEL_NO, "recaChannelNo", 1);
		dc.createAlias(Recruitapplier.PROP_RECA_INTERVIEWER, "recaInterviewer",
				1);
		dc.createAlias(Recruitapplier.PROP_RECA_REFER_EMP_NO, "recaReferEmpNo",
				1);
		addCriteria(applier, dc);
		dc.add(Restrictions.in(Recruitapplier.PROP_RECA_STATUS,
				APPLIER_STATUS_T));
		checkOrderMethod(dc, page.getOrder());
		page.splitPage(dc);
		return applierDAO.findByCriteria(dc, page.getPageSize(), page
				.getCurrentPage());
	}

	private void addCriteria(Recruitapplier applier, DetachedCriteria dc) {
		if (applier != null) {
			if (applier.getRecaName() != null
					&& applier.getRecaName().trim().length() > 0)
				dc.add(Restrictions.like("recaName", applier.getRecaName()
						.trim(), MatchMode.ANYWHERE));
			if (applier.getRecaPlanId() != null
					&& applier.getRecaPlanId().getRecpDepartmentNo() != null
					&& applier.getRecaPlanId().getRecpDepartmentNo().getId() != null
					&& applier.getRecaPlanId().getRecpDepartmentNo().getId()
							.trim().length() > 0)
				dc
						.add(Restrictions
								.sqlRestriction(
										"reca_plan_id in(select recp_id from recruitplan where recp_department_no in (select department_no from department where department_no =?))",
										applier.getRecaPlanId()
												.getRecpDepartmentNo().getId()
												.trim(), Hibernate.STRING));
			if (applier.getRecaStatus() != null)
				dc
						.add(Restrictions
								.sqlRestriction(
										"reca_id in(select reca_id from recruitapplier where reca_status = ?)",
										applier.getRecaStatus(),
										Hibernate.INTEGER));
		}
	}

	private void checkOrderMethod(DetachedCriteria detachedCriteria,
			String order) {
		if (order != null && order.trim().length() > 1
				&& order.indexOf('-') != -1) {
			String pram[] = order.split("-");
			if (pram[0] == null || pram[0].length() < 1)
				return;
			String fetch[] = pram[0].split("\\.");
			String orde = pram[0];
			if (fetch.length > 1) {
				String str = "";
				String fetc = "";
				for (int len = 0; len < fetch.length - 1; len++) {
					detachedCriteria.createAlias((new StringBuilder()).append(
							fetc).append(fetch[len]).toString(),
							(new StringBuilder()).append("ord").append(len)
									.toString());
					fetc = (new StringBuilder()).append(fetc)
							.append(fetch[len]).append(".").toString();
					str = (new StringBuilder()).append("ord").append(len)
							.append(".").toString();
				}

				orde = (new StringBuilder()).append(str).append(
						pram[0].substring(pram[0].lastIndexOf(".") + 1))
						.toString();
			}
			if (pram.length == 2 && pram[1].equalsIgnoreCase("up"))
				detachedCriteria.addOrder(Order.asc(orde));
			else
				detachedCriteria.addOrder(Order.desc(orde));
		} else {
			detachedCriteria.addOrder(Order.desc("recaInterviewTime"));
		}
	}

	public List searchApplier(Recruitapplier applier, Pager page, String id) {
		DetachedCriteria dc = DetachedCriteria
				.forClass(com.wyzz.hr.pojo.Recruitapplier.class);
		for (int i = 0; i < fetch.length; i++)
			dc.setFetchMode(fetch[i], FetchMode.JOIN);

		dc.add(Restrictions.eq("recaPlanId.id", id));
		return applierDAO.findByCriteria(dc);
	}

	public List insertApplier(Recruitapplier applier, String empNo) {
		List errors = new ArrayList();
		try {
			Employee employee = new Employee(empNo);
			applier.setRecaCreateBy(employee);
			applier.setRecaLastChangeBy(employee);
			applier.setRecaCreateTime(new Date());
			applier.setRecaLastChangeTime(new Date());
			applierDAO.saveOrupdate(applier);
			return errors;
		} catch (Exception e) {
			e.printStackTrace();
		}
		errors.add("\u6DFB\u52A0\u62DB\u8058\u6210\u5458\u5931\u8D25\uFF01");
		return errors;
	}

	public List updateApllier(Recruitapplier applier, String empNo) {
		List errors = new ArrayList();
		try {
			Employee employee = new Employee(empNo);
			applier.setRecaLastChangeBy(employee);
			applier.setRecaLastChangeTime(new Date());
			applierDAO.saveOrupdate(applier);
			return errors;
		} catch (Exception e) {
			e.printStackTrace();
		}
		errors.add("\u66F4\u65B0\u62DB\u8058\u6210\u5458\u5931\u8D25\uFF01");
		return errors;
	}

	public IRecruitapplierDAO getApplierDAO() {
		return applierDAO;
	}

	public void setApplierDAO(IRecruitapplierDAO applierDAO) {
		this.applierDAO = applierDAO;
	}

	public List getApplierStatus() {
		StatusDAOImpl statusDAO = (StatusDAOImpl) SpringBeanFactory
				.getBean("statusDAO");
		return statusDAO.getStatusByTable("recruitapplier");
	}

	public List getAllAplierDepartment() {
		DetachedCriteria dc = DetachedCriteria
				.forClass(com.wyzz.hr.pojo.Recruitapplier.class);
		dc.setFetchMode("recaPlanId", FetchMode.JOIN);
		dc.setFetchMode("recaPlanId.recpDepartmentNo", FetchMode.JOIN);
		return applierDAO.findByCriteria(dc);
	}

	public void updateApplierRemark(String comment, Integer status,
			Recruitapplier applier, String id[]) {
		if (id == null)
			return;
		String planId = null;
		String interviewId = null;
		Date interviewTime = null;
		if (applier != null) {
			planId = applier.getRecaPlanId().getId();
			if (StringUtils.isEmpty(planId))
				planId = null;
			interviewId = applier.getRecaInterviewer().getId();
			if (StringUtils.isEmpty(interviewId))
				interviewId = null;
			if (applier.getRecaInterviewTime() != null)
				interviewTime = applier.getRecaInterviewTime();
		}
		List applierList = loadApplierByIds(id);
		if (applierList.isEmpty())
			return;
		Iterator i$ = applierList.iterator();
		do {
			if (!i$.hasNext())
				break;
			Recruitapplier r = (Recruitapplier) i$.next();
			if (StringUtils.isEmpty(r.getRecaComment())) {
				r.setRecaComment(comment);
			} else {
				String str = r.getRecaComment();
				if (!StringUtils.isEmpty(comment))
					r.setRecaComment((new StringBuilder()).append(str).append(
							";").append(comment).toString());
			}
			r.setRecaInterviewTime(interviewTime);
			r.setRecaLastChangeTime(new Date());
			r.setRecaStatus(status);
			if (planId != null)
				r.setRecaPlanId(new Recruitplan(planId));
			if (interviewId != null)
				r.setRecaInterviewer(new Employee(interviewId));
		} while (true);
		saveOrUpdateApplier(applierList);
	}

	public List loadApplierByIds(String ids[]) {
		DetachedCriteria dc = DetachedCriteria
				.forClass(com.wyzz.hr.pojo.Recruitapplier.class);
		dc.createAlias(Recruitapplier.PROP_RECA_PLAN_ID, "recaPlanId", 1);
		dc.createAlias((new StringBuilder()).append(
				Recruitapplier.PROP_RECA_PLAN_ID).append(".").append(
				Recruitplan.PROP_RECP_PB_NO).toString(), "recpPbNo", 1);
		dc.createAlias((new StringBuilder()).append(
				Recruitapplier.PROP_RECA_PLAN_ID).append(".").append(
				Recruitplan.PROP_RECP_TYPE).toString(), "recpType", 1);
		dc.createAlias((new StringBuilder()).append(
				Recruitapplier.PROP_RECA_PLAN_ID).append(".").append(
				Recruitplan.PROP_RECP_WORK_LOCATION).toString(),
				"recpWorkLocation", 1);
		dc.createAlias(Recruitapplier.PROP_RECA_CHANNEL_NO, "recaChannelNo", 1);
		dc.createAlias(Recruitapplier.PROP_RECA_CREATE_BY, "recaCreateBy", 1);
		org.hibernate.criterion.Criterion tempc = Restrictions.in(
				Recruitapplier.PROP_ID, ids);
		dc.add(tempc);
		return applierDAO.findByCriteria(dc);
	}

	public Integer getNumberOfApplierInPlan(String PlanId) {
		DetachedCriteria dc = DetachedCriteria
				.forClass(com.wyzz.hr.pojo.Recruitapplier.class);
		dc.add(Restrictions.eq("recaPlanId.id", PlanId));
		dc.add(Restrictions.eq("recaStatus", Integer.valueOf(13)));
		return Integer.valueOf(applierDAO.findRowCountByCriteria(dc));
	}

	public List searchApplierByDc(DetachedCriteria dc, Pager page,
			String searchOrExport) {
		if (page == null || "export".equals(searchOrExport)) {
			return applierDAO.findByCriteria(dc);
		} else {
			page.splitPage(dc);
			return applierDAO.findByCriteria(dc, page.getPageSize(), page
					.getCurrentPage());
		}
	}

	public List loadApplierByDc(DetachedCriteria dc, Pager page) {
		dc.createAlias(Recruitapplier.PROP_RECA_PLAN_ID, "recaPlanId", 1);
		dc.createAlias((new StringBuilder()).append(
				Recruitapplier.PROP_RECA_PLAN_ID).append(".").append(
				Recruitplan.PROP_RECP_PB_NO).toString(), "recpPbNo", 1);
		dc.createAlias((new StringBuilder()).append(
				Recruitapplier.PROP_RECA_PLAN_ID).append(".").append(
				Recruitplan.PROP_RECP_TYPE).toString(), "recpType", 1);
		dc.createAlias((new StringBuilder()).append(
				Recruitapplier.PROP_RECA_PLAN_ID).append(".").append(
				Recruitplan.PROP_RECP_WORK_LOCATION).toString(),
				"recpWorkLocation", 1);
		dc.createAlias(Recruitapplier.PROP_RECA_CHANNEL_NO, "recaChannelNo", 1);
		dc.createAlias(Recruitapplier.PROP_RECA_CREATE_BY, "recaCreateBy", 1);
		BaseAction.addOrders(dc, page, new String[] { (new StringBuilder())
				.append(Recruitapplier.PROP_RECA_LAST_CHANGE_TIME).append(
						"-down").toString() });
		page.splitPage(dc);
		return applierDAO.findByCriteria(dc, page.getPageSize(), page
				.getCurrentPage());
	}

	public Recruitapplier loadApplierById(String applierId) {
		DetachedCriteria dc = DetachedCriteria
				.forClass(com.wyzz.hr.pojo.Recruitapplier.class);
		dc.createAlias(Recruitapplier.PROP_RECA_PLAN_ID, "recaPlanId", 1);
		dc.createAlias((new StringBuilder()).append(
				Recruitapplier.PROP_RECA_PLAN_ID).append(".").append(
				Recruitplan.PROP_RECP_DEPARTMENT_NO).toString(),
				"recpDepartmentNo", 1);
		dc.createAlias((new StringBuilder()).append(
				Recruitapplier.PROP_RECA_PLAN_ID).append(".").append(
				Recruitplan.PROP_RECP_PB_NO).toString(), "recpPbNo", 1);
		dc.createAlias((new StringBuilder()).append(
				Recruitapplier.PROP_RECA_PLAN_ID).append(".").append(
				Recruitplan.PROP_RECP_TYPE).toString(), "recpType", 1);
		dc.createAlias((new StringBuilder()).append(
				Recruitapplier.PROP_RECA_PLAN_ID).append(".").append(
				Recruitplan.PROP_RECP_WORK_LOCATION).toString(),
				"recpWorkLocation", 1);
		dc.createAlias(Recruitapplier.PROP_RECA_CHANNEL_NO, "recaChannelNo", 1);
		dc.createAlias(Recruitapplier.PROP_RECA_INTERVIEWER, "recaInterviewer",
				1);
		dc.createAlias(Recruitapplier.PROP_RECA_REFER_EMP_NO, "recaReferEmpNo",
				1);
		dc.createAlias(Recruitapplier.PROP_RECA_RELATION_EMP_NO,
				"recaRelationEmpNo", 1);
		BaseCrit.addDC(dc, Recruitapplier.PROP_ID, "eq",
				new String[] { applierId });
		for (int i = 0; i < fetch.length; i++)
			dc.setFetchMode(fetch[i], FetchMode.JOIN);
		return (Recruitapplier) applierDAO.findByCriteria(dc).get(0);
	}

	public void saveOrUpdateApplier(List applierList) {
		applierDAO.saveOrupdate(applierList);
	}

	public List loadAllApplier() {
		DetachedCriteria dc = DetachedCriteria
				.forClass(com.wyzz.hr.pojo.Recruitapplier.class);
		dc.createAlias(Recruitapplier.PROP_RECA_PLAN_ID, "recaPlanId", 1);
		dc.createAlias((new StringBuilder()).append(
				Recruitapplier.PROP_RECA_PLAN_ID).append(".").append(
				Recruitplan.PROP_RECP_DEPARTMENT_NO).toString(),
				"recpDepartmentNo", 1);
		dc.createAlias((new StringBuilder()).append(
				Recruitapplier.PROP_RECA_PLAN_ID).append(".").append(
				Recruitplan.PROP_RECP_TYPE).toString(), "recpType", 1);
		dc.createAlias((new StringBuilder()).append(
				Recruitapplier.PROP_RECA_PLAN_ID).append(".").append(
				Recruitplan.PROP_RECP_WORK_LOCATION).toString(),
				"recpWorkLocation", 1);
		dc.createAlias(Recruitapplier.PROP_RECA_CHANNEL_NO, "recaChannelNo", 1);
		for (int i = 0; i < fetch.length; i++)
			dc.setFetchMode(fetch[i], FetchMode.JOIN);

		return applierDAO.findByCriteria(dc);
	}

	public Map countApplierByPlan() {
		Map result = new HashMap();
		String hql = "select recaPlanId.id,count(*) from Recruitapplier where recaPlanId.id is not null and recaStatus in (16,17) group by recaPlanId.id";
		List list = applierDAO.exeHqlList(hql);
		if (list.isEmpty())
			return null;
		Object res[];
		for (Iterator i$ = list.iterator(); i$.hasNext(); result.put(res[0]
				.toString(), Integer.valueOf(Integer
				.parseInt(res[1].toString())))) {
			Object obj = i$.next();
			res = (Object[]) (Object[]) obj;
		}

		return result;
	}

	private IRecruitapplierDAO applierDAO;
	private Integer APPLIER_STATUS_T[] = { Integer.valueOf(16),
			Integer.valueOf(18), Integer.valueOf(21), Integer.valueOf(22) };
	String fetch[] = { "recaCreateBy", "recaChannelNo", "recaLastChangeBy",
			"recaPlanId", "recaPlanId.recpType", "recaPlanId.recpPbNo",
			"recaPlanId.recpDepartmentNo", "recaPlanId.recpWorkLocation" };
}
